package com.ls.knowledge.llm.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @BelongsProject: knowledge
 * @BelongsPackage: com.ls.knowledge.llm.config
 * @Author: lishun
 * @CreateTime: 2025-06-30  23:55
 * @Description:
 * @Version: 1.0
 */
@Configuration
@MapperScan("com.ls.knowledge.llm.mapper")
public class SpringAiConfig {


    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(1000)
                .build();
    }

    @Bean
    public ChatClient chatClient(
                                 ChatModel chatModel,
                                 ChatMemory chatMemory
    ) {
        return ChatClient.builder(chatModel)
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                .build();
    }
}
